<template>
	<view class="main-content">
		<scroll-view scroll-y :style="{height: scrollHeight}" class="list-box">
			<text class="title">{{ownerComName || ''}}</text>
			<uni-forms ref="masterEnterForm" :modelValue="FormData" :rules="rules" label-position="top"
				label-width="300rpx">
				<uni-forms-item label="维修工时(小时)" class="form-item-vertical" name="manHour">
					<input type="number" placeholder="请输入" v-model="FormData.manHour" />
					<view class="line"></view>
				</uni-forms-item>
				<uni-forms-item label="工时费用" class="form-item-vertical" name="manHourCost">
					<view style="display: flex;align-items: center;">
						<input type="text" @input="validateInput1" placeholder="请输入" v-model="FormData.manHourCost" />
						<view>元</view>
					</view>
					<view class="line"></view>
				</uni-forms-item>
				<uni-forms-item class="form-item-vertical" name="switch">
					<template slot="label">
						<view class="switch-view">
							<text>设备更换费用</text>
							<text @click="addSwitch" style="color: #325EF5">+ 添加</text>
						</view>
					</template>
					<view class="switch">
						<view class="switch-view row-center-random" v-for="(item,index) in FormData.deviceRenewInfos"
							:key="index">
							<view class="switch-view-two row-center-random">
								<input class="switch-view-two-ipt" type="text" placeholder="请输入设备名称"
									v-model="FormData.deviceRenewInfos[index].deviceRenewInfo" />
								<view class="switch-input-view row-center-random">
									<input class="switch-view-two-ipt" type="text" placeholder="请输入设备价格"
										v-model="FormData.deviceRenewInfos[index].deviceRenewMoney"
										@input="validateInput(index)" />
									<text>元</text>
								</view>
							</view>
							<text class="switch-view-text" v-if="FormData.deviceRenewInfos.length!==index"
								@click="deleteSwitch(index)">- 清除</text>
						</view>
					</view>
					<text v-show="false">{{total}}</text>
				</uni-forms-item>
			</uni-forms>
		</scroll-view>
		<you-bottom-btn :btnConfig="bottomBtnConfig" @formSubmit="formSubmit" @testReport="testReport">
		</you-bottom-btn>
	</view>
</template>

<script>
	import Home from "@/api/home.js"
	export default {
		data() {
			return {
				FormData: {
					manHour: '',
					manHourCost: '',
					deviceRenewInfos: [{
						deviceRenewInfo: '',
						deviceRenewMoney: '',
					}],
				},
				rules: {
					manHour: {
						rules: [{
							required: true,
							errorMessage: '请输入维修工时',
						}]
					},
					manHourCost: {
						rules: [{
							required: true,
							errorMessage: '请输入工时费用',
						}]
					},
				},
				scrollHeight: '',
				bottomBtnConfig: [{
					text: '总计：0元',
					class: 'exhibition',
					funcName: ''
				}, {
					text: '提交',
					class: 'confirm',
					funcName: 'formSubmit'
				}],
				orderId: '',
				ownerComName: ''
			}
		},
		onLoad(e) {
			this.scrollHeight = this.$store.getters.heightData.noTabMainHeight - 74 + 'px'
			this.orderId = e.id;
			this.ownerComName = e.ownerComName;
		},
		computed: {
			//总计
			total() {
				let price = 0
				this.FormData.deviceRenewInfos.forEach((el) => {
					price += Number(el.deviceRenewMoney)
				})
				this.bottomBtnConfig[0].text = `总计：${this.FormData.manHour * this.FormData.manHourCost + Number(price)}元`
				console.log(this.bottomBtnConfig[0].text);
				return this.FormData.manHour * Number(this.FormData.manHourCost) + Number(price)
			}
		},
		methods: {
			validateInput1() {
				const value = this.FormData.manHourCost
				// 使用正则表达式检查是否为数字或包含小数点的数字
				const isValid = (/[0-9]*[.,]?[0-9]+/g).test(value);
				if (!isValid) {
					console.log(this.FormData.manHourCost, 11111);
					this.$nextTick(() => {
						this.FormData.manHourCost = ""
					})
					// uni.showToast({
					// 	title: '请输入有效的数字',
					// 	icon: 'none'
					// });
				}
			},
			validateInput(index) {
				const value = this.FormData.deviceRenewInfos[index].deviceRenewMoney;
				// 使用正则表达式检查是否为数字或包含小数点的数字
				const isValid = (/[0-9]*[.,]?[0-9]+/g).test(value);

				if (!isValid) {
					this.$nextTick(() => {
						this.$set(this.FormData.deviceRenewInfos, index, {
							...this.FormData.deviceRenewInfos[index],
							deviceRenewMoney: ""
						});
					})
					// console.log(this.FormData.deviceRenewInfos[index]);

					// console.log(this.FormData.deviceRenewInfos[index],11111);
					// // const deviceRenewInfo = this.FormData.deviceRenewInfos[index];
					// // this.$set(deviceRenewInfo, 'deviceRenewMoney', " ");
					// // 如果你想在用户输入无效字符时给出提示，可以使用uni.showToast
					// uni.showToast({
					// 	title: '请输入有效的数字',
					// 	icon: 'none'
					// });
				}
			},

			//添加设备更换
			addSwitch() {
				this.FormData.deviceRenewInfos.push({
					deviceRenewInfo: '',
					deviceRenewMoney: '',
				})
			},
			//清除设备更换
			deleteSwitch(index) {
				this.FormData.deviceRenewInfos.splice(index, 1)
			},
			formSubmit() {
				
				this.$refs.masterEnterForm.validate().then(res => {
					if(this.FormData.manHourCost==0){
						uni.showToast({
							title: '工时费用最少0.01元',
							icon: 'none'
						});
						return
					}
					this.FormData.deviceRenewInfos = this.FormData.deviceRenewInfos.filter(obj => !(obj
						.deviceRenewInfo === "" && obj.deviceRenewMoney === ""))
					console.log(this.FormData, 'this.FormData');
					Home.updateMasterOrderStatus({
						...this.FormData,
						status: 4,
						id: this.orderId
					}).then(res => {
						uni.showToast({
							title: "提交成功"
						})
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/home'
							})
						}, 1500)

					})
				}).catch(err => {
					console.log('表单错误信息：', err);
				})
			}
		}
	}
</script>

<style scoped lang="less">
	.main-content {
		width: 100%;
		height: 100%;
		padding: 20rpx 30rpx;

		.list-box {
			padding: 0 20rpx;
			border-radius: 16rpx;
			background: #ffffff;

			.line {
				border-bottom: 2rpx solid #e6e6e6;
				margin: 20rpx 0;
			}

			/deep/.uni-forms-item {
				margin-bottom: 0;
			}

			.title {
				padding: 20rpx 0;
				display: block;
				border-bottom: 2rpx solid #e6e6e6;
				font-family: "PingFang SC";
				font-size: 28rpx;
				font-style: normal;
				font-weight: 600;
				line-height: 40rpx;
				opacity: 0.9;
				margin-bottom: 20rpx;
			}

			.switch-view {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-top: 20rpx;

				.switch-view-two {
					display: flex;
					justify-content: space-between;
					width: calc(100% - 100rpx);

					.switch-view-two-ipt {
						width: calc(50% - 12rpx);
						height: 60rpx;
						padding: 12rpx;
						background: #f6f8fa;
						border-radius: 10rpx;
						font-size: 26rpx;
						margin-right: 32rpx;
					}

					.switch-input-view {
						background-color: #f6f8fa;
						width: calc(50%);
						padding-right: 10rpx;
						box-sizing: border-box;
						border-radius: 10rpx;

						.switch-view-two-ipt {
							width: 90%;
							margin-right: 0;

						}
					}
				}

				.switch-view-text {
					&:last-child {
						display: block;
						width: 100rpx;
						color: #325ef5;
						text-align: right;
						font-family: "PingFang SC";
						font-size: 28rpx;
						font-style: normal;
						font-weight: 400;
						line-height: 40rpx;
					}
				}
			}
		}
	}
</style>